package com.claimSettlement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.claimSettlement.mapper.PolicyholdersMapper;
import com.claimSettlement.pojo.Policyholders;
import com.claimSettlement.service.PolicyholdersService;
import com.claimSettlement.utils.JsonUtils;
import com.github.pagehelper.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 投保人信息业务层
 * 曹文韬 -- 2021-05-19
 */
@Service
public class PolicyholdersServiceImpl implements PolicyholdersService {
    @Resource
    private PolicyholdersMapper policyholdersMapper;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public List<Policyholders> getPolicyholdersAll() {
        redisTemplate.delete("policList");
        List<Policyholders> list = null;
        if(redisTemplate.hasKey("policList")){
            list = JsonUtils.jsonToList(redisTemplate.opsForHash().values("policList").toString(),Policyholders.class);
        }else{
            QueryWrapper<Policyholders> queryWrapper = new QueryWrapper();
            queryWrapper.orderByAsc("policyholdersId");
            list = policyholdersMapper.selectList(queryWrapper);
            Map<String,String> map = new HashMap();
            for (Policyholders policyholders: list) {
                map.put(String.valueOf(policyholders.getPolicyholdersId()),JsonUtils.objectToJson(policyholders));
            }
            redisTemplate.opsForHash().putAll("policList",map);
        }

        return list;
    }

    public List<Policyholders> selectPolicyholdersByNameOrCredenId(String name, String credenId) {
        return policyholdersMapper.selectPolicyholdersByNameOrCredenId(name, credenId);
    }

    public Policyholders getPolicyholdersById(int policyholdersId) {
        return policyholdersMapper.selectById(policyholdersId);
    }

    public int insPolicyholders(Policyholders policyholders) {
        int count = policyholdersMapper.insert(policyholders);
        if(count > 0){
            redisTemplate.delete("policList");
            //redisTemplate.opsForHash().put("policList",policyholders.getPolicyholdersId(),JsonUtils.objectToJson(policyholders));
        }
        return count;
    }

    public int updPolicyholders(Policyholders policyholders) {
        int count = policyholdersMapper.updateById(policyholders);
        if(count > 0){
            redisTemplate.opsForHash().put("policList",policyholders.getPolicyholdersId(),JsonUtils.objectToJson(policyholders));
        }
        return count;
    }

    public int delPolicyholders(int policyholdersId) {
        int count = policyholdersMapper.deleteById(policyholdersId);
        if(count > 0){
            redisTemplate.delete("policList");
        }
        return count;
    }

    public List<Policyholders> getPolicyholdersNameAndId() {
        return policyholdersMapper.getPolicyholdersNameAndId();
    }
}
